<?php

namespace app\guild\controller;


use app\common\Esclient;
use app\common\model\LiveModel;
use app\common\model\LiveRoomBanneduserModel;
use app\common\model\UserModel;
use think\facade\Request;

class ChatController extends BaseController
{
    /**
     * 直播间聊天日志监控
     */
    public function live_chat()
    {
        // 查询直播列表
        $page  = Request::param('page',1);
        $limit = 12;


        // 所属代理员工
        $gulid_idarr = $this->getGuildUserId();
        $lives       = array();
        $count       = 0;
        if ($gulid_idarr) {
            $where = [
                ['anchorid', 'in', $gulid_idarr]
            ];
            $lives = LiveModel::where($where)->with('anchor')->order(['id' => 'desc'])->limit(($page - 1) * $limit, $limit)->select();
            $count = LiveModel::where($where)->count('*');
        }

        $this->assign('lives', $lives);
        $this->assign('page', $page);
        $this->assign('limit', $limit);
        $this->assign('count', $count);

        return $this->fetch();
    }

    /**
     * 直播聊天监控日志列表
     * @return \think\response\Json
     */
    public function get_live_chat()
    {
        $room_id = Request::param('room_id', 0);
        $chat_id = Request::param('chat_id', 0);
        $page    = Request::param("page", 1);
        $limit   = Request::param("limit", 20);

        if ($room_id < 1) {
            return parent::bulidSuccess();
        }

        // es查询条件
        $options = [
            'index' => 'es_chat_history',
            'size'  => $limit,
            'from'  => ($page - 1) * $limit,
            'sort'  => ['id' => 'desc'],
            'where' => [
                ['room_id', '=', $room_id],
            ],
        ];

        // 从那个id开始查询
        if ($chat_id) {
            $options['where'][] = ['id', '>', $chat_id];
        }

        // 获取数据
        $es_client = new Esclient();
        $response  = $es_client->search($options);

        $data  = $response['data'];
        $count = $response['total'];

        if ($count > 0) {
            $uid_arr = array_column($data, 'user_id');
            $uid_arr = array_unique(array_filter($uid_arr));

            $user_list = UserModel::whereIn('id', $uid_arr)->field('id,nick_name')->select()->toArray();
            $user_list = array_column($user_list, 'nick_name', 'id');

            // 禁用的用户
            $bannkd_user = LiveRoomBanneduserModel::with('mgruser')->whereIn('uid', $uid_arr)
                ->where('ban_time', 0)
                ->whereOr('ban_time', '>=', time())
                ->select()->toArray();

            foreach ($data as &$item) {
                $item['status'] = 1;
                // 用户封禁状态，-1未封禁，0=超管，>0房管
                $item['mgrid']     = -1;
                $item['mgrname']   = '';
                $item['nick_name'] = isset($item['user_id']) && !empty($user_list[$item['user_id']]) ? $user_list[$item['user_id']] : '';

                foreach ($bannkd_user as $val) {
                    // 所有房间
                    if ($val['anchorid'] == 0 && isset($item['user_id']) && $val['uid'] == $item['user_id']) {
                        $item['status']  = 2;
                        $item['mgrid']   = $val['mgrid'];
                        $item['mgrname'] = '超管';
                    }
                    // 某一个房间
                    if ($val['anchorid'] == $item['room_id'] && isset($item['user_id']) && $val['uid'] == $item['user_id']) {
                        $item['status']  = 2;
                        $item['mgrid']   = $val['mgrid'];
                        $item['mgrname'] = isset($val['mgruser']) ? $val['mgruser']['nick_name'] : '';
                    }
                }
            }
        }
        return json(["code" => 0, "msg" => "", "data" => $data]);
    }


}